package co.za.itweb.itwebpro;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class SingleAdapter extends BaseAdapter {
	private Activity activity2;
	String[] itemID;
	String[] titles;
	String[]images;
	String[] summary;
	private LayoutInflater inflater;
	@Override
	public int getCount() {
		return itemID.length;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	

	public SingleAdapter(Activity act,String[] item,String[] tit, String[] img,String[] sum) {
	titles=tit;
	images=img;
	summary=sum;
	itemID=item;
	activity2=act;
	inflater = (LayoutInflater) activity2
			.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
	
	private static class ViewHolder {
		public ImageView iconView;
		public TextView titleTextView;
		public TextView bottomText;
		public  ImageLoader imageLoader;
		 public LazyLoadImage icondownloader;
		
	}


	@Override
	public View getView(int position, View convertView, final ViewGroup parent) {
		ViewHolder holder;
		
		
    // if (convertView == null) {
    	  convertView = (ViewGroup) inflater.inflate(R.layout.imageslist_single, null);
			holder = new ViewHolder();
      

			holder.titleTextView = (TextView) convertView
					.findViewById(R.id.TextOnImage);
			holder.bottomText = (TextView) convertView
				.findViewById(R.id.TextTitle);
			holder.iconView = (ImageView) convertView
					.findViewById(R.id.oneimage);
			holder.imageLoader=new ImageLoader(activity2);
			holder.icondownloader=new LazyLoadImage(holder.iconView);
			convertView.setTag(holder);
			convertView.setId(Integer.parseInt(itemID[position]));
			
		//	}else {
			
		//		holder = (ViewHolder) convertView.getTag();
			
				  
		//	}
      
     
				holder.iconView.setPadding(1, 1, 1, 1);
				
				
				if(images[position].length()>4){
					//BitmapManager.INSTANCE.loadBitmap("http://itweb.mobi/__itweb_pro/feed/iosv2/cache/img"+images[position], holder.iconView, 200, 200);
					 //holder.imageLoader.DisplayImage("http://itweb.mobi/__itweb_pro/feed/iosv2/cache/img"+images[position], holder.iconView);
				
				
				Bitmap avatar =
						holder.icondownloader.getBitmapFromCache(MainActivity.cachedir+"/"+images[position]);
				
				 if (avatar == null) {
					 holder.icondownloader.execute(MainActivity.cachedir+"/"+images[position]);
				
				} else {
					
					holder.iconView.setImageBitmap(avatar);
				}
				 holder.titleTextView.setText(titles[position]);
				 holder.bottomText.setVisibility(View.GONE);
				}else{
				 holder.iconView.setBackgroundColor(Color.parseColor("#ff050505"));
				 if( summary[position].length()>90 ){
					String it= summary[position].substring(0,90);
						it=it+"...";
						holder.titleTextView.setText(it);
						}else{
							holder.titleTextView.setText(summary[position]);
						}
	
						
						holder.titleTextView.setHeight(250);
						
						holder.bottomText.setText(titles[position]);
						holder.bottomText.setTypeface(null, Typeface.BOLD);	
				}
				//convertView.setClickable(true);
//			final int pos=position;
//				convertView.setOnClickListener( new OnClickListener() {
//					public void onClick(View v) {
//			               v.setBackgroundColor(Color.parseColor("#ff376ccd"));
//			              int offsetY = ((v.getTop() + v.getBottom()) / 2) - (parent.getHeight() / 2);
//			              parent.scrollTo(0,offsetY);
//			    }
//			});
				
		return convertView;

	}
	
	
	
}
